package stream;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 *
 * Collectors提供了一系列用于数据统计的静态方法：
 *
 * 计数：count
 * 平均值：averagingInt、averagingLong、averagingDouble
 * 最值：maxBy、minBy
 * 求和：summingInt、summingLong、summingDouble
 * 统计以上所有：summarizingInt、summarizingLong、summarizingDouble
 *
 * @author Hongliang Zhu
 * @create 2021-08-01 15:59
 */
public class Demo8 {
    public static void main(String[] args) {
//        案例：统计员工人数、平均工资、工资总额、最高工资。

        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));

        // 总数
        Long count = personList.stream().collect(Collectors.counting());
        System.out.println(count);

        //  平均工资
        Double avg = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
        System.out.println(avg);

        // 最高工资
        Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
        System.out.println(max.get());


        // 工资之和
        IntSummaryStatistics collect = personList.stream().map(Person::getSalary).collect(Collectors.summarizingInt(Integer::intValue));
        System.out.println(collect.getSum());


        // 一次性统计所有信息
        System.out.println(personList.stream().collect(Collectors.summarizingInt(Person::getSalary)));

    }

}
